package com.wisewin.circle.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import java.io.File;
import java.net.URL;

public class VideoUtil {
    public static DefaultAcsClient init(String accessKeyId, String accessKeySecret,String token){
        String regionId="cn-beijing";//接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret,token);
        return new DefaultAcsClient(profile);
    }

    /**
     * 获取视频时长：小时
     *
     * @param file
     * @return
     */
    public static Float getVideoTime(File file) {
        try {
            MultimediaObject instance = new MultimediaObject(file);
            MultimediaInfo result = instance.getInfo();
            // 毫秒 -> 秒 -> 分钟 -> 小时
            Float ls = result.getDuration() / 1000.0f / 60.0f / 60.0f;
            return ls;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0.0f;
    }

    public static Long getVideoTimeByURL(String path) {
        try {
            URL url = new URL(path) ;
            MultimediaObject instance = new MultimediaObject(url);
            MultimediaInfo result = instance.getInfo();
            // 毫秒 -> 秒 -> 分钟 -> 小时
            long ls = result.getDuration();
            return ls;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public static Long getVideoTime(String path) {
        try {
            URL url = new URL(path) ;
            MultimediaObject instance = new MultimediaObject(url);
            MultimediaInfo result = instance.getInfo();
            // 毫秒 -> 秒 -> 分钟 -> 小时
            long ls = result.getDuration() / 1000;
            return ls;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public static void main(String[] args) {
        System.out.println(VideoUtil.getVideoTimeByURL("https://health-mirror.oss-cn-beijing.aliyuncs.com/actions/test.mp4"));
    }
}
